package com.zjsru.plan2023.oneday.oneDay202301;

/**
 * @Author: CookLee
 * @Date: 2023/1/16
 * 句子相似性 III
 *
 * 输入：sentence1 = "My name is Haley", sentence2 = "My Haley"
 * 输出：true
 * 解释：可以往 sentence2 中 "My" 和 "Haley" 之间插入 "name is" ，得到 sentence1 。
 *
 * 输入：sentence1 = "of", sentence2 = "A lot of words"
 * 输出：false
 * 解释：没法往这两个句子中的一个句子只插入一个句子就得到另一个句子。
 *
 * 输入：sentence1 = "Eating right now", sentence2 = "Eating"
 * 输出：true
 * 解释：可以往 sentence2 的结尾插入 "right now" 得到 sentence1 。
 *
 * 输入：sentence1 = "Luky", sentence2 = "Lucccky"
 * 输出：false
 */
public class AreSentencesSimilar {
    
    /**
     * 句子相似:双指针
     *
     * @param sentence1 sentence1
     * @param sentence2 sentence2
     * @return boolean
     */
    public boolean areSentencesSimilar(String sentence1, String sentence2) {
        String[] sentence1Split = sentence1.split(" ");
        String[] sentence2Split = sentence2.split(" ");
        //定义双指针i j;
        //i表示两个字符串数组从左开始，最多有 i 个字符串相同
        //j 表示剩下的字符串数组从右开始，最多有 j 个字符串相同
        int i = 0, j = 0;
        while (i < sentence1Split.length && i < sentence2Split.length && sentence1Split[i].equals(sentence2Split[i])) {
            i++;
        }
        while (j < sentence1Split.length - i && j < sentence2Split.length - i &&
                sentence1Split[sentence1Split.length - j - 1].equals(sentence2Split[sentence2Split.length - j - 1])) {
            j++;
        }
        //i + j 即表示从左 和 从右开始相同的字符串数字和
        return i + j == Math.min(sentence1Split.length, sentence2Split.length);
    }
    
    public static void main(String[] args) {
        AreSentencesSimilar areSentencesSimilar = new AreSentencesSimilar();
        String sentence1 = "Eating right now";
        String sentence2 = "Eating";
        System.out.println(areSentencesSimilar.areSentencesSimilar(sentence1, sentence2));
    }
}
